package com.amgl.mobike.module.search;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.amgl.mobike.R;
import com.amgl.mobike.app.IImageLoader;
import com.amgl.mobike.base.BaseRVAdapter;
import com.amgl.mobike.base.BaseViewHolder;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 阿木 on 2017/5/27.
 */

public class ImageRVAdapter extends BaseRVAdapter<ImageData> {
    public ImageRVAdapter(Context context, IImageLoader imageLoader) {
        super(context, imageLoader);
    }

    @Override
    public BaseViewHolder<ImageData> onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = createItemView(R.layout.rv_item_image, parent);
        ImageViewHolder vh = new ImageViewHolder(itemView);
        return vh;
    }

    public class ImageViewHolder extends BaseViewHolder<ImageData> {
        @BindView(R.id.image_view)
        ImageView mImageView;

        @BindView(R.id.text_title)
        TextView mTextView;

        public ImageViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void showData(ImageData data) {
            if (getImageLoader() != null) {
                Uri uri = Uri.parse(mData.getImageThumb());
                getImageLoader().loadImage(mImageView, uri);
            }

            String title = mData.getTitle();
            if (TextUtils.isEmpty(title)) {
                mTextView.setVisibility(View.GONE);
            } else {
                mTextView.setText(title);
                mTextView.setVisibility(View.VISIBLE);
            }
        }
    }
}
